class Scroll_Map
    attr_accessor
    def initialize(map,view)
      @l=$game_variables[296]
      @r=$game_variables[297]
      @map = map.clone
      @tileset = RPG::Cache.tileset($game_map.tileset_name)
      @sprite = []
      for i in 0...@map.width
        @sprite[i]=[]
        for j in 0...@map.height
          @sprite[i][j]=[]
          for k in 0...3
            if i>=5 && @map.data[i,j,k]>0
              tid = @map.data[i,j,k]
              if k>0
                @sprite[i][j][k] = Sprite_ScrollTile.new(view,i,j,k,tid,@l,@r,0)
              else
                @sprite[i][j][k] = Sprite_ScrollTile.new(view,i,j,k,tid,@l,@r,1)
              end
              tid-=384
              @sprite[i][j][k].bitmap = @tileset
              @sprite[i][j][k].src_rect = Rect.new(tid%8*32,tid/8*32,32,32)
            end
          end
        end
      end
      @shadowL=[]
      @shadowR=[]
      len=(@r-@l)/4
      for i in 0...@map.height
        @shadowL[i]=Sprite.new(view)
        @shadowL[i].bitmap=RPG::Cache.picture("towerShadow")
        @shadowL[i].src_rect=Rect.new(0,0,32,32)
        @shadowL[i].x=400-len*32
        @shadowL[i].ox=16
        @shadowL[i].visible=false
        @shadowR[i]=Sprite.new(view)
        @shadowR[i].bitmap=RPG::Cache.picture("towerShadow")
        @shadowR[i].src_rect=Rect.new(96,0,32,32)
        @shadowR[i].x=400+len*32
        @shadowR[i].ox=16
        @shadowR[i].visible=false
      end
      @shadowLL=[]
      @shadowRR=[]
      for i in 0...@map.height
        @shadowLL[i]=Sprite.new(view)
        @shadowLL[i].bitmap=RPG::Cache.picture("towerShadow")
        @shadowLL[i].src_rect=Rect.new(32,0,32,32)
        @shadowLL[i].x=400-(len-1)*32
        @shadowLL[i].y=i*32
        @shadowLL[i].ox=16
        @shadowRR[i]=Sprite.new(view)
        @shadowRR[i].bitmap=RPG::Cache.picture("towerShadow")
        @shadowRR[i].src_rect=Rect.new(64,0,32,32)
        @shadowRR[i].x=400+(len-1)*32
        @shadowRR[i].y=i*32
        @shadowRR[i].ox=16
      end
    end
    def start(csprite,player)
      @character_sprite = csprite
      @player_sprite = player
    end
    def update
      for i in 0...@map.width
        for j in 0...@map.height
          for k in 0...3
            if @sprite[i][j][k] != nil
              @sprite[i][j][k].update
            end
          end
        end
      end
      len=(@r-@l)/4
      for i in 0...@map.height
        x=$game_player.x-len
        if x<@l
          x+=@r-@l
        end
        @shadowL[i].y=(i * 128 - $game_map.display_y + 3) / 4
        xl=x-1
        xr=x+1
        if xr>=@r
          xr-=@r-@l
        end
        if xl<@l
          xl+=@r-@l
        end
        if @sprite[x][i][1]
          @shadowL[i].visible=true
          rect=@sprite[x][i][1].src_rect
          if !@sprite[xr][i][1] && $game_player.direction==4
            @shadowL[i].src_rect=Rect.new(rect.x%32,0,rect.width,32)
            @shadowL[i].x=400-len*32+@sprite[x][i][1].x%32
          elsif !@sprite[xl][i][1] && $game_player.direction==6
            @shadowL[i].src_rect=Rect.new(0,0,32-@sprite[x][i][1].x%32,32)
            @shadowL[i].x=400-len*32+@sprite[x][i][1].x%32
          else
            @shadowL[i].src_rect=Rect.new(0,0,32,32)
            @shadowL[i].x=400-len*32
          end
          @shadowL[i].z=@sprite[x][i][1].z
        else
          if @sprite[xr][i][1]
            whr=@sprite[xr][i][1].x%32
          end
          if @sprite[xl][i][1]
            whl=@sprite[xl][i][1].x%32
          end
          if @sprite[xr][i][1] && whr>0 && $game_player.direction==4
            @shadowL[i].visible=true
            @shadowL[i].src_rect=Rect.new(0,0,32-whr,32)
            @shadowL[i].x=400-len*32+whr
            @shadowL[i].z=@sprite[xr][i][1].z
          elsif @sprite[xl][i][1] && whl>0 && $game_player.direction==6
            @shadowL[i].visible=true
            @shadowL[i].src_rect=Rect.new(0,0,32-whl,32)
            @shadowL[i].x=400-len*32+whl
            @shadowL[i].z=@sprite[xl][i][1].z
          else
            @shadowL[i].visible=false
          end
        end
        x=$game_player.x+len
        if x>=@r
          x-=@r-@l
        end
        @shadowR[i].y=(i * 128 - $game_map.display_y + 3) / 4
        xl=x-1
        xr=x+1
        if xr>=@r
          xr-=@r-@l
        end
        if xl<@l
          xl+=@r-@l
        end
        if @sprite[x][i][1]
          @shadowR[i].visible=true
          rect=@sprite[x][i][1].src_rect
          if !@sprite[xr][i][1] && $game_player.direction==4
            @shadowR[i].src_rect=Rect.new(96,0,@sprite[x][i][1].x%32 == 0 ? 32 : @sprite[x][i][1].x%32,32)
            @shadowR[i].x=400+len*32
          elsif !@sprite[xl][i][1] && $game_player.direction==6
            @shadowR[i].src_rect=Rect.new(rect.x%32,0,rect.width,32)
            @shadowR[i].x=400+len*32+@sprite[x][i][1].x%32
          else
            @shadowR[i].src_rect=Rect.new(96,0,32,32)
            @shadowR[i].x=400+len*32
          end
          @shadowR[i].z=@sprite[x][i][1].z
        else
          if @sprite[xr][i][1]
            whr=@sprite[xr][i][1].src_rect.width%32
          end
          if @sprite[xl][i][1]
            whl=@sprite[xl][i][1].x%32
          end
          if @sprite[xr][i][1] && whr>0 && $game_player.direction==4
            @shadowR[i].visible=true
            @shadowR[i].src_rect=Rect.new(96,0,whr,32)
            @shadowR[i].x=400+len*32
            @shadowR[i].z=@sprite[xr][i][1].z
          elsif @sprite[xl][i][1] && whl>0 && $game_player.direction==6
            @shadowR[i].visible=true
            @shadowR[i].src_rect=Rect.new(96,0,whl,32)
            @shadowR[i].x=400+len*32
            @shadowR[i].z=@sprite[xl][i][1].z
          else
            @shadowR[i].visible=false
          end
        end
      end
      len=(@r-@l)/4-1
      xl=$game_player.x-len
      xr=$game_player.x+len
      if xr>=@r
        xr-=@r-@l
      end
      if xl<@l
        xl+=@r-@l
      end
      for i in 0...@map.height
        @shadowLL[i].z=@sprite[xl][i][0].z
        @shadowRR[i].z=@sprite[xr][i][0].z
        @shadowLL[i].y=@sprite[xl][i][0].y
        @shadowRR[i].y=@sprite[xr][i][0].y
      end
      @player_sprite.x=400
    end
    def dispose
      for i in 0...@map.width
        for j in 0...@map.height
          for k in 0...3
            if @sprite[i][j][k] != nil
              @sprite[i][j][k].dispose
            end
          end
        end
      end
      for i in 0...@map.height
        @shadowL[i].dispose
        @shadowLL[i].dispose
        @shadowR[i].dispose
        @shadowRR[i].dispose
      end
      @tileset.dispose
      @sprite = []
    end
  end
  class Sprite_ScrollTile < RPG::Sprite
    attr_accessor :sx
    attr_accessor :sy
    attr_accessor :sk
    attr_accessor :sz
    attr_accessor :type
    attr_accessor :time
    def initialize(view,sx,sy,sk,tile_id,l,r,t)
      super(view)
      @type = 0
      @sx= sx
      @sy = sy
      @sk = sk
      @sz = $game_map.priorities[tile_id] * 32 + 32
      if $game_map.priorities[tile_id] == 0
        @sz = 0
      end
      @tid=tile_id
      @len=r-l
      xx=sx-1
      if xx<l
        xx+=@len
      end
      if $game_map.data[xx,sy,sk]>0
        @llink=true
      else
        @llink=false
      end
      xx=sx+1
      if xx>=r
        xx-=@len
      end
      if $game_map.data[xx,sy,sk]>0
        @rlink=true
      else
        @rlink=false
      end
      @t=t
      update
    end
    def update
      super
      x=@sx
      if x-$game_player.x>@len/2
        x=x-@len
      end
      if $game_player.x-x>@len/2
        x=x+@len
      end
      x=x-$game_player.x
      if x.abs>@len/4-@t+1
        xx=@len/2-x
        xx=xx*32+384
        self.visible=false
      else
        self.visible=true
        xx=x*32+384
      end
      tid=@tid-384
      self.src_rect=Rect.new(tid%8*32,tid/8*32,32,32)
      pt=$game_player.real_x%128/4
      if $game_player.direction==2 || $game_player.direction==8
        if x.abs==@len/4-@t+1
          self.visible=false
        end
      end
      zz=0
      if $game_player.direction==4
        if x==@len/4-@t+1
          self.visible=false
          if pt>0
            if @llink
              self.src_rect=Rect.new(tid%8*32,tid/8*32,pt,32)
            else
              xx+=pt-32
              self.src_rect=Rect.new(32-pt+tid%8*32,tid/8*32,pt,32)
            end
            self.visible=true
          end
        end
        if -x==@len/4-@t+1
          self.visible=false
        end
        if -x==@len/4-@t
          if pt>0 
            if @rlink
              self.src_rect=Rect.new(pt+tid%8*32,tid/8*32,32-pt,32)
              xx+=pt
            else
              xx+=pt*2
              self.src_rect=Rect.new(tid%8*32,tid/8*32,32-pt,32)
            end
          end
        end
      end
      if $game_player.direction==6
        if pt>0
          xx+=32
        end
        if -x==@len/4-@t+1
          self.visible=false
          if pt>0
            if @rlink
              self.src_rect=Rect.new(pt+tid%8*32,tid/8*32,32-pt,32)
              xx+=pt
            else
              xx+=pt*2
              self.src_rect=Rect.new(tid%8*32,tid/8*32,32-pt,32)
            end
            self.visible=true
          end
        end
        if x==@len/4-@t+1
          self.visible=false
        end
        if x==@len/4-@t
          if pt>0
            if @llink
              self.src_rect=Rect.new(tid%8*32,tid/8*32,pt,32)
            else
              xx+=pt-32
              self.src_rect=Rect.new(32-pt+tid%8*32,tid/8*32,pt,32)
            end
            self.visible=true
          end
        end
      end
      self.x = xx-pt
      self.y = (@sy * 128 - $game_map.display_y + 3) / 4
      self.z = @sz + 32 + (@sy * 128 - $game_map.display_y + 3) / 4
    end
  end